
package com.cptravel.activity.order;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.core.api.ApiListenrDefaultImpl;
import com.core.api.B5MBaseResponse;
import com.core.controller.B5MBaseActivity;
import com.core.eventbus.EventBus;
import com.core.util.B5MToaster;
import com.core.util.TimeUtils;
import com.cptravel.R;
import com.cptravel.api.PostorderRequest;
import com.cptravel.api.UpdateorderRequest;
import com.cptravel.bean.Order;
import com.cptravel.bean.Xiangxi;
import com.cptravel.tool.BaseDateTimePickerDialog;
import com.cptravel.tool.EventNames;
import com.cptravel.tool.UserInfoManage;

public class BookJiuDianActivity extends B5MBaseActivity
{
    private TextView tv_jiudianname, tv_type;
    private EditText et_arrivtedatetime, et_leavedatetime, et_count, et_people, et_contactname, et_totalmoney, et_tel, et_email, et_mark;
    private PostorderRequest mPostorderRequest;
    private BaseDateTimePickerDialog mBaseDialog;
    private int type;//1arrivete2leave
    private Order mOrder;
    private UpdateorderRequest mUpdateorderRequest;
    private Button bt_commit;
    private Xiangxi xiangxi;

    @Override
    protected String getHeadTitle()
    {
        // TODO Auto-generated method stub
        return "订单编辑";
    }

    @Override
    public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.book_jiudian;
    }

    @Override
    public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
        tv_jiudianname = (TextView) findViewById(R.id.tv_name);
        tv_type = (TextView) findViewById(R.id.tv_type);
        et_arrivtedatetime = (EditText) findViewById(R.id.et_arrivtedatetime);
        et_leavedatetime = (EditText) findViewById(R.id.et_leavedatetime);
        et_count = (EditText) findViewById(R.id.et_count);
        et_people = (EditText) findViewById(R.id.et_checkinname);
        et_contactname = (EditText) findViewById(R.id.et_contactname);
        et_totalmoney = (EditText) findViewById(R.id.et_totalmoney);
        et_tel = (EditText) findViewById(R.id.et_tel);
        et_email = (EditText) findViewById(R.id.et_email);
        et_mark = (EditText) findViewById(R.id.et_mark);
        bt_commit = (Button) findViewById(R.id.bt_commit);
        et_arrivtedatetime.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                type = 1;
                if (mBaseDialog.isShowing())
                {
                    mBaseDialog.dismiss();
                }
                else
                {
                    mBaseDialog.show();
                }
            }
        });
        et_leavedatetime.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                type = 2;
                if (mBaseDialog.isShowing())
                {
                    mBaseDialog.dismiss();
                }
                else
                {
                    mBaseDialog.show();
                }
            }
        });
        mBaseDialog = BaseDateTimePickerDialog.createConfirmDialog(mContext);
        mBaseDialog.setOnLeftButtonClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                mBaseDialog.dismiss();
                Calendar calendar = Calendar.getInstance();
                calendar.set(mBaseDialog.datePicker.getYear(), mBaseDialog.datePicker.getMonth(), mBaseDialog.datePicker.getDayOfMonth(), mBaseDialog.timePicker.getCurrentHour(), mBaseDialog.timePicker.getCurrentMinute());
                String dateTime = TimeUtils.getTime(calendar.getTimeInMillis(), new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA));
                if (type == 1)
                {
                    et_arrivtedatetime.setText(dateTime);
                }
                else
                {
                    et_leavedatetime.setText(dateTime);
                }
            }
        });
        et_count.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void onTextChanged(CharSequence s , int start , int before , int count)
            {
                // TODO Auto-generated method stub
                if (s != null && !TextUtils.isEmpty(s))
                {
                    et_totalmoney.setText(Double.valueOf(s.toString()) * Double.valueOf(isUpdate() ? mOrder.price : xiangxi.price) + "");
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s , int start , int count , int after)
            {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s)
            {
                // TODO Auto-generated method stub
            }
        });
    }

    public boolean isUpdate()
    {
        return mOrder != null;
    }

    @Override
    public void doInitDataes()
    {
        // TODO Auto-generated method stub
        mOrder = (Order) getIntent().getSerializableExtra("order");
        if (isUpdate())
        {
            bt_commit.setText("确认修改");
            et_arrivtedatetime.setText(mOrder.intime);
            et_contactname.setText(mOrder.contact);
            et_count.setText(mOrder.number+"");
            et_leavedatetime.setText(mOrder.outtime);
            et_email.setText(mOrder.email);
            et_mark.setText(mOrder.note);
            et_tel.setText(mOrder.phone);
            mUpdateorderRequest = new UpdateorderRequest(new ApiListenrDefaultImpl()
            {
                @Override
                public void onSuccess(B5MBaseResponse b5mBaseResponse)
                {
                    // TODO Auto-generated method stub
                    super.onSuccess(b5mBaseResponse);
                    finish();
                }
            }, mProgressBar);
        }
        else
        {
            xiangxi = (Xiangxi) getIntent().getSerializableExtra("xiangxi");
            tv_title.setText(xiangxi.title);
            //
            tv_jiudianname.setText(xiangxi.title);
            tv_type.setText(xiangxi.ticketstype);
            mPostorderRequest = new PostorderRequest(new ApiListenrDefaultImpl()
            {
                @Override
                public void onSuccess(B5MBaseResponse b5mBaseResponse)
                {
                    // TODO Auto-generated method stub
                    super.onSuccess(b5mBaseResponse);
                    B5MToaster.showShort(mActivity, "订单提交成功", 0);
                    finish();
                    EventBus.getInstance().notifiDataUpdate(EventNames.ORDER_ORDER_SUCCESS,null);
                }
            }, mProgressBar);
        }
    }

    @Override
    public void doAfter()
    {
        // TODO Auto-generated method stub
    }

    public void commit(View view)
    {
        if (!UserInfoManage.getInstance().checkIsLogined(mActivity))
        {
            return;
        }
        String arrivtedatetime = et_arrivtedatetime.getText().toString().trim();
        if (TextUtils.isEmpty(arrivtedatetime))
        {
            B5MToaster.showShort(mActivity, "到店时间不能为空", 0);
            return;
        }
        String leavedatetime = et_leavedatetime.getText().toString().trim();
        if (TextUtils.isEmpty(leavedatetime))
        {
            B5MToaster.showShort(mActivity, "离店时间不能为空", 0);
            return;
        }
        String number = et_count.getText().toString().trim();
        if (TextUtils.isEmpty(number))
        {
            B5MToaster.showShort(mActivity, "房间数不能为空", 0);
            return;
        }
        String people = et_people.getText().toString().trim();
        if (TextUtils.isEmpty(people))
        {
            B5MToaster.showShort(mActivity, "入住人不能为空", 0);
            return;
        }
        String contact = et_contactname.getText().toString().trim();
        if (TextUtils.isEmpty(contact))
        {
            B5MToaster.showShort(mActivity, "联系人姓名不能为空", 0);
            return;
        }
        String phone = et_tel.getText().toString().trim();
        if (TextUtils.isEmpty(phone))
        {
            B5MToaster.showShort(mActivity, "联系人电话不能为空", 0);
            return;
        }
        String email = et_email.getText().toString().trim();
        String note = et_mark.getText().toString().trim();
        if (isUpdate())
        {
            mUpdateorderRequest.orderid = mOrder.orderid;
            mUpdateorderRequest.intime = arrivtedatetime;
            mUpdateorderRequest.outtime = leavedatetime;
            mUpdateorderRequest.number = number;
            mUpdateorderRequest.inperson = people;
            mUpdateorderRequest.contact = contact;
            mUpdateorderRequest.classid = "1";
            mUpdateorderRequest.pid = mOrder.pid;
            mUpdateorderRequest.ticketstype = mOrder.ticketstype;
            mUpdateorderRequest.price = mOrder.price;
            mUpdateorderRequest.phone = phone;
            mUpdateorderRequest.email = email;
            mUpdateorderRequest.note = note;
            mUpdateorderRequest.start();
        }
        else
        {
            mPostorderRequest.intime = arrivtedatetime;
            mPostorderRequest.outtime = leavedatetime;
            mPostorderRequest.number = number;
            mPostorderRequest.inperson = people;
            mPostorderRequest.contact = contact;
            mPostorderRequest.classid = "1";
            mPostorderRequest.pid = xiangxi.pid;
            mPostorderRequest.ticketstype = xiangxi.ticketstype;
            mPostorderRequest.price = xiangxi.price;
            mPostorderRequest.phone = phone;
            mPostorderRequest.email = email;
            mPostorderRequest.note = note;
            mPostorderRequest.start();
        }
    }
}
